from lxml import etree
import urllib.request

# xpath解析所有的标签必须有结尾

# 解析文件  xpath方法中需要书写xpath语法
# etree.parse() 解析本地标签

# 查找所有的有id的li标签
# li_list = tree.xpath('//li[@id]')

# 查找所有的li标签中id为a2的标签
# li_list = tree.xpath('//li[@id="a2"]')

# 查询li标签中id以a开头的
# li_list = tree.xpath('//li[start-with(@id, "a"]')

# 查询li标签中id包含1的标签
# li_list = tree.xpath('//li[contains(@id, "1")]')

url = 'https://car.autohome.com.cn/price/brand-33.html'
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}

request = urllib.request.Request(url=url, headers=headers)
response = urllib.request.urlopen(request)
content = response.read().decode('gb2312', 'ignore')

# 解析
# name //div[@class="list-cont-main"]/div/a/text()
# src //div[@class="list-cont-img"]/a/img/@src
# price //div[@class="main-lever-right"]/div//span[@class="font-arial"]/text()

tree = etree.HTML(content)
src_list = tree.xpath('//div[@class="list-cont-img"]/a/img/@src')
name_list = tree.xpath('//div[@class="list-cont-main"]/div/a/text()')
price_list = tree.xpath('//div[@class="main-lever-right"]/div//span[@class="font-arial"]/text()')

print(len(src_list), len(name_list), len(price_list))

for i in range(len(src_list)):
    src = src_list[i]
    name = name_list[i]
    price = price_list[i]
    print(src, name, price)

